package com.hngm.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.hngm.bean.Product;
import com.hngm.bean.Producttypetable;
import com.hngm.bean.User;
import com.hngm.common.Result;
import com.hngm.common.ResultList;
import com.hngm.dao.UserDao;
import com.hngm.util.StringUtil;

/**
 * 用户登陆的servle
 */
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		//获取前端传递过来的url
		String url = request.getParameter("url");
		UserDao userdao = new UserDao();
		if("login".equals(url)){
			//用户登陆的逻辑
			//获取用户名密码
			String username = request.getParameter("username");
			String pwd = request.getParameter("pwd");
			HttpSession session = request.getSession();
			if(StringUtil.isNotNullorEmpty(username) && StringUtil.isNotNullorEmpty(pwd)){
				//查询数据库，判断该用户是否为空
				User user = userdao.queryUserByUsernameAndPwd(username, pwd);
				if(user == null){
					out.print(Result.toClient(false, "用户名或者密码有误"));				
				}else{
					//需要将用户信息存储到session作用域中
					session.setAttribute("userInfo", user);
					out.print(Result.toClient(true, "登陆成功！"));
				}
				
			}else{
				//用户名密码有误
				out.print(Result.toClient(false, "用户名密码有误"));
			}
			
		}else if("queryUsertableListByParam".equals(url)){
			//获取前端传过来的查询条件
			String username = request.getParameter("username");
		    String orderby = request.getParameter("orderby");
			
			//获取LayUI穿过来的分页信息
			int limit = Integer.parseInt(request.getParameter("limit"));
			int page = Integer.parseInt(request.getParameter("page"));
			
			//当前页开始的索引
			int beginIndex = (page - 1) * limit;
			
			//根据传过来得查询条件去查询产品列表
			ArrayList<User> pList = userdao.queryuserListByParm(username,orderby,beginIndex,limit);
			
			//查询数据库中的数量
			int count = userdao.queyruserListByParamCount(username,orderby);
			
			//返回给调用者一个JSON格式的字符串
			//将获取到的产品列表转换成符合Layui要求格式的JSON字符串
			String str = ResultList.toClient(0, "成功", count, pList);
			out.print(str);
		
		}else if("add".equals(url)){
			//添加用户信息逻辑
			//1、获取前端传过来的信息
			String username = request.getParameter("username");
			String pwd = request.getParameter("pwd");
			User p = new User();
			p.setUsername(username);
			p.setPwd(pwd);
			//2、产品类型信息插入到表中（调用DAO）
			int result = userdao.insertUser(p);
			//3、返回结果给前端

			if(result > 0){
				//成功 返回JSON类型的处理结果
				out.print(Result.toClient(true, "添加成功"));
				
			}else{
				//失败
				out.print(Result.toClient(false, "添加失败"));
			}
		
		}else if("del".equals(url)){
			//删除用户逻辑
			//1、获取前端传过来的产品Id
			String userId = request.getParameter("userId");
			//2、根据产品Id删除产品信息
			int result = UserDao.delProduct(userId);
			//3、将删除的结果返回给前端页面
			if(result > 0){
				out.print(Result.toClient(true, "删除成功"));
			}else{
				out.print(Result.toClient(false, "删除失败"));
			}
		
		}else if("edit".equals(url)){

			//添加用户信息逻辑
			//1、获取前端传过来的信息
			long id = Long.parseLong(request.getParameter("id"));
			String username = request.getParameter("username");
			String pwd = request.getParameter("pwd");
			User p = new User();
			p.setUsername(username);
			p.setPwd(pwd);
			p.setId(id);
			//2、产品类型信息插入到表中（调用DAO）
			int result = userdao.editUser(p);
			//3、返回结果给前端

			if(result > 0){
				//成功 返回JSON类型的处理结果
				out.print(Result.toClient(true, "编辑成功"));
				
			}else{
				//失败
				out.print(Result.toClient(false, "编辑失败"));
			}
		
		
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
